package com.example.definedview.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.definedview.R;
import com.example.definedview.view.bean.ScheduleBean;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    List<ScheduleBean> scheduleBeanList = new ArrayList<>();

    public void setScheduleBeanList(List<ScheduleBean> scheduleBeanList) {
        this.scheduleBeanList = scheduleBeanList;
    }

    //创建viewholder时呼叫
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.cell, parent, false);
        return new MyViewHolder(itemView);
    }

    //viewholder和recycleview绑定时呼叫
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ScheduleBean scheduleBean = scheduleBeanList.get(position);
        holder.textView.setText(scheduleBean.getText());
        holder.icon.setImageResource(scheduleBean.getIcon());
        holder.state.setImageResource(scheduleBean.getState());
    }

    //返回列表数据的个数
    @Override
    public int getItemCount() {
        return scheduleBeanList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView icon, state;
        TextView textView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            state = itemView.findViewById(R.id.state);
            textView = itemView.findViewById(R.id.stateText);
        }
    }
}
